package com.he.controller;

import com.he.annotation.AccessLimit;
import com.he.domain.Result;
import com.he.domain.dto.CheckDTO;
import com.he.domain.dto.ConditionDTO;
import com.he.domain.dto.MessageDTO;
import com.he.domain.dto.PageResult;
import com.he.domain.vo.MessageBackVO;
import com.he.service.MessageService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author He
 * @version 1.0
 * @Date 2023/9/22 15:24
 * @Desc 留言controller
 */
@Tag(name = "留言模块")
@RestController
public class MessageController {

    @Resource
    private MessageService messageService;

    /**
     * 查看留言列表
     *
     * @return {@link Result<>}
     */
    @Operation( description = "查看留言列表")
    @GetMapping("/message/list")
    public Result<?> getMessageList(){
        return Result.success(messageService.listMessageVO());
    }

    /**
     * 添加留言
     *
     * @param message 留言信息
     * @return {@link Result<>}
     */
    @AccessLimit(seconds = 60, maxCount = 3)
    @Operation(description = "添加留言")
    @PostMapping("/message/add")
    public Result<?> addMessage(@Validated @RequestBody MessageDTO message) {
        messageService.addMessage(message);
        return Result.success();
    }

    /**
     * 查看后台留言列表
     *
     * @param condition 条件
     * @return {@link Result<MessageBackVO>} 留言列表
     */
    @Operation(description = "查看后台留言列表")
    @PreAuthorize("@ps.hasPermission('news:message:list')")
    @GetMapping("/admin/message/list")
    public Result<PageResult<MessageBackVO>> listMessageBackVO(ConditionDTO condition) {
        return Result.success(messageService.listMessageBackVO(condition));
    }

    /**
     * 删除留言
     *
     * @param messageIdList 留言Id列表
     * @return {@link Result<>}
     */
    @Operation(description = "删除留言")
    @PreAuthorize("@ps.hasPermission('news:message:delete')")
    @DeleteMapping("/admin/message/delete")
    public Result<?> deleteMessage(@RequestBody List<Integer> messageIdList) {
        messageService.removeByIds(messageIdList);
        return Result.success();
    }

    /**
     * 审核留言
     *
     * @param check 审核信息
     * @return {@link Result<>}
     */
    @Operation(description = "审核留言")
    @PreAuthorize("@ps.hasPermission('news:message:pass')")
    @PutMapping("/admin/message/pass")
    public Result<?> updateMessageCheck(@Validated @RequestBody CheckDTO check) {
        messageService.updateMessageCheck(check);
        return Result.success();
    }

    @Operation(description = "查看留言壁纸")
    @GetMapping("/message/wallpaper")
    public Result<String> getMessageWallpaper() {
        return Result.success(messageService.getMessageWallpaper());
    }

}
